from rest_framework import serializers
from django_redis import get_redis_connection


class RegisterSmscodeSerializer(serializers.Serializer):
    text=serializers.CharField(max_length=4,min_length=4,required=True,label='图片验证码')
    image_code_id=serializers.UUIDField(label='uuid')

    def validate(self, attrs):
        text=attrs.get('text')
        # print(type(text))
        image_code_id=attrs.get('image_code_id')

        redis_conn=get_redis_connection('code')
        redis_text=redis_conn.get('img_%s'%image_code_id)
        # print(type(redis_text))

        if redis_text is None:
            raise serializers.ValidationError('图片验证码以过期')

        if text.lower()!=redis_text.decode().lower():
            raise serializers.ValidationError('输入的验证码不一致')

        # print('done')

        return attrs